

package dao

import (
	"errors"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/frame/gmvc"
	"guli-shopping/product-service/protobuff/productProto"
)

var (
	 //直接填充,this里也能用
	 PmsAttrAttrgroupRelationDao = &pmsattrattrgrouprelationDao{
		 M:g.DB().Table("pms_attr_attrgroup_relation").Safe(),
	 }
)

type pmsattrattrgrouprelationDao struct {
	gmvc.M
}

func (this *pmsattrattrgrouprelationDao) FindAll(query *productProto.PmsAttrAttrgroupRelationQuery) ([]*productProto.PmsAttrAttrgroupRelation,int,error) {
    //分页,如果要添加条件,在这里添加
    count, _ := this.OmitEmpty().Count()
	all, err := this.OmitEmpty().Page(int(query.Page), int(query.Size)).All()
	models := []*productProto.PmsAttrAttrgroupRelation{}
	err = all.Structs(&models)
	if err != nil {
		return nil,0,errors.New("查询失败")
	}
	return models,count,err


}

func (this *pmsattrattrgrouprelationDao) FindById(id int64) (*productProto.PmsAttrAttrgroupRelation,error) {
	all, err := this.WherePri(id).FindOne()
	if err != nil {
		return nil,errors.New("查询失败")
	}
	models := &productProto.PmsAttrAttrgroupRelation{}
	all.Struct(models)
	return models,err
}

func (this *pmsattrattrgrouprelationDao) Delete(ids[]int64) error {
	all, _ := this.WherePri(ids).Count()
	//要删除的数量不匹配
	if  len(ids)!=all{
		return errors.New("删除失败")
	}
	_, err := this.WherePri(ids).Delete()
	return err
}

func (this *pmsattrattrgrouprelationDao) Update(req *productProto.PmsAttrAttrgroupRelation) error {
	_, err := this.OmitEmpty().WherePri(req.Id).Data(req).Update()
	return err
}
func (this *pmsattrattrgrouprelationDao) Save(req []*productProto.PmsAttrAttrgroupRelation) (error) {
	all, err := this.Data(req).Save()
	if err!=nil {
		return errors.New("保存失败")
	}
	count, _ := all.RowsAffected()
	if count<=0 {
		return errors.New("可能重复添加")
	}
	return nil
}

func (this *pmsattrattrgrouprelationDao) FindAllNoAssociatedAttr(id int64,atrrType int32) ([]*productProto.PmsAttr,error) {
	//查询出所有未关联的销售属性,一定要是销售属性
	sql:="SELECT * FROM `pms_attr` WHERE attr_id NOT IN (SELECT attr_id FROM `pms_attr_attrgroup_relation` WHERE attr_group_id=?) AND attr_type=?"
	models := []*productProto.PmsAttrAttrgroupRelation{}
	all, _ := g.DB().GetAll(sql, id,atrrType)
	err := all.Structs(&models)
	if err!=nil {
		return nil,errors.New("查询失败"+err.Error())
	}

	ids:=[]int32{}
	for _, value := range models {
		ids=append(ids,value.AttrId)
	}

	attrList, _ := g.DB().Table("pms_attr").WherePri(ids).All()
	attrs := []*productProto.PmsAttr{}
	err = attrList.Structs(&attrs)
	if err != nil {
		return nil,errors.New("查询失败")
	}
	return attrs,nil
}

func (this *pmsattrattrgrouprelationDao) FindAllAssociatedAttr(id int64,attrType int32) ([]*productProto.PmsAttr,error) {
	all, _ := this.Where("attr_group_id", id).All()
	models := []*productProto.PmsAttrAttrgroupRelation{}
	err := all.Structs(&models)
	if err != nil {
		return nil,errors.New("查询失败")
	}
	ids:=[]int32{}
	for _, value := range models {
		ids=append(ids,value.AttrId)
	}
	attrList, _ := g.DB().Table("pms_attr").WherePri(ids).And("attr_type",attrType).All()
	attrs := []*productProto.PmsAttr{}
	err = attrList.Structs(&attrs)
	if err != nil {
		return nil,errors.New("查询失败")
	}
	return attrs,nil
}




